package ThreadTest.Synchronized;
/*
synchronized面试题
    下面程序的doOther方法需要等待doSome方法执行完毕后才能执行吗
    不是，doOther没有synchronized关键字修饰，所以在执行时并不会去找共享对象锁
    所以不用等待doSome方法执行完毕
 */
public class Test1 {
    public static void main(String[] args) {
        MyClass m = new MyClass();
        MyClassThread myClassThread = new MyClassThread(m);
        Thread t1 = new Thread(myClassThread);
        Thread t2 = new Thread(myClassThread);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }
}
class MyClassThread implements Runnable{
    MyClass mc;
    public MyClassThread(MyClass mc){
        this.mc = mc;
    }
    public void run(){
        if(Thread.currentThread().getName().equals("t1")){
            mc.doSome();
        }else {
            mc.doOther();
        }
    }
}
class MyClass{
    public synchronized void doSome(){
        System.out.println("doSome begin");
        try {
            Thread.sleep(1000*10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }

    public void doOther(){
        System.out.println("doOther begin");
        System.out.println("doOther over");
    }
}